package com.bajiao.app.controller.common;

import cn.hutool.core.map.MapUtil;
import com.bajiao.common.api.vo.RestResult;
import com.bajiao.common.menu.ResultEnum;
import com.bajiao.third.utils.OSSClientUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;


/**
 * @Description: 通用控制器
 * @Author: FanJq
 * @Date: 2024-10-10
 * @Version: V1.0
 */

@RestController
@RequestMapping("/api/v1/common")
@Api(tags = {"通用接口"})
public class RestCommonController {


    @PostMapping(value = "/uploadImagesAli")
    @ApiOperation(value = "上传多图片到阿里云OSS", tags = {"通用接口"}, notes = "上传多图片到阿里云OSS")
    public RestResult<?> uploadImageAli(@RequestParam(value = "files") MultipartFile[] files) {
        String result = "";
        try {
            result = OSSClientUtils.multipleFileUploadNew(files);
            return RestResult.success(ResultEnum.OPERATION_SUCCESS.getDesc(), result);
        } catch (Exception e) {
            e.printStackTrace();
            return RestResult.error();
        }
    }

    @PostMapping(value = "/fileUploadAli")
    @ApiOperation(value = "上传文件到阿里云OSS", tags = {"通用接口"}, notes = "上传文件到阿里云OSS")
    public RestResult<?> fileUpload(@RequestParam(value = "file") MultipartFile file) {
        try {
            String fileName = file.getOriginalFilename().trim();
            if (StringUtils.isBlank(fileName)) {
                return RestResult.success();
            }
            InputStream fileContent = file.getInputStream();
            String url = OSSClientUtils.uploadFile(fileContent, fileName);
            return RestResult.success(MapUtil.builder("url",url).build());
        } catch (Exception e) {
            e.getMessage();
            return RestResult.error();
        }
    }
}
